%{ This program loads the baseline result and evaluates several counterfactuals
%}

clear all;
clc;
close all;

%%%%%%%%%%%%%% Sharpen Baseline %%%%%%%%%%%%%%%%%%%%
load results/resultsBaseline.mat
save latest.mat lambd policy dist V_old
params = ([8.6 1.7 20000 .3]);
fun(params,'Baseline'); % saves resultsBaseline 


%%%%%%%%%%%%%%%%%%%% Flexible commission benchmark (spec 3) %%%%%%%%%%%%%%%%%%%%
clear;
load results/resultsBaseline.mat
num_buyers = sum(repmat(num_b',1,kw).*w_mat);
%load results/resultsBaseline_flexcommission.mat ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit
buyerfix = 0;
params = ([8.6 1.7 20000 .3]);
model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,'Baseline',3, buyerfix, num_buyers);

%%% compute an average commission for the flexible commission
load results/resultsBaseline_flexcommission.mat
sagent_commission_profit =  sum(((Pr/kw).*Listings),2).*((Ne./(Ne+Nb))'.*Sale_p).*Psi_flex.*Price_vec'; % commissions per agents
sagent_profit_exp = sagent_commission_profit.*dist'; % total commissions by experience
tot_list_commissions_state = sagent_profit_exp'*w_mat; % total commissions by state

sagent_sales =  sum(((Pr/kw).*Listings),2).*((Ne./(Ne+Nb))'.*Sale_p).*Price_vec'; % sales volume per agent
sagent_sales_exp = sagent_sales.*dist'; % total sales volume by experience
tot_sales_volume_state = sagent_sales_exp'*w_mat; % total sales volume by state

avg_commission_st = tot_list_commissions_state./tot_sales_volume_state; % average commission by state 
avg_psi = q*avg_commission_st'; % average commission in the economy (1.53%)


clearvars -except avg_psi;
load results/resultsBaseline.mat 
Vb_guess = zeros(1,9);
num_buyers = sum(repmat(num_b',1,kw).*w_mat);
%load results/resultsBaselineBuyerfix_flexcommission.mat ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess
buyerfix = 1;
params = ([8.6 1.7 20000 .3]);
model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,'BaselineBuyerfix',3, buyerfix, num_buyers,  Vb_guess);
%%% compute an average commission for the flexible commission
load results/resultsBaselineBuyerfix_flexcommission.mat
sagent_commission_profit =  sum(((Pr/kw).*Listings),2).*((Ne./(Ne+Nb))'.*Sale_p).*Psi_flex.*Price_vec'; % commissions per agents
sagent_profit_exp = sagent_commission_profit.*dist'; % total commissions by experience
tot_list_commissions_state = sagent_profit_exp'*w_mat; % total commissions by state

sagent_sales =  sum(((Pr/kw).*Listings),2).*((Ne./(Ne+Nb))'.*Sale_p).*Price_vec'; % sales volume per agent
sagent_sales_exp = sagent_sales.*dist'; % total sales volume by experience
tot_sales_volume_state = sagent_sales_exp'*w_mat; % total sales volume by state

avg_commission_st = tot_list_commissions_state./tot_sales_volume_state; % average commission by state 
avg_psi_buyerfix = q*avg_commission_st'; % average commission in the economy (1.61%)


%%%%%%%%%% Counterfactual - fixed commission rates at avg. of the efficient equilibrium %%%%%%%%%%%%%%%%%%%%%
clearvars -except avg_psi avg_psi_buyerfix; 
load results/resultsBaseline.mat 
Vb_guess = zeros(1,9); % doesn't matter for this one      
num_buyers = sum(repmat(num_b',1,kw).*w_mat);   
buyerfix = 0;
psi = avg_psi;
params = ([8.6 1.7 20000 .3]);

saveas = strcat('CommissionEffAvg');
filename = strcat('resultsCommissionEffAvg.mat'); 
model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,saveas,0, buyerfix, num_buyers, Vb_guess);   


load results/resultsBaseline.mat 
Vb_guess = zeros(1,9); % doesn't matter for this one      
num_buyers = sum(repmat(num_b',1,kw).*w_mat);   
buyerfix = 1;
psi = avg_psi_buyerfix;
params = ([8.6 1.7 20000 .3]);

saveas = 'CommissionBuyerfixEffAvg';
filename = 'resultsCommissionBuyerfixEffAvg.mat'; 
model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,saveas,0, buyerfix, num_buyers, Vb_guess);   


%%%%%%%%%%%%%%%%%%%%%% Main counterfactual: change entry cost %%%%%%%%%%%%%%%%%%%%
clear;
entry_array = 1:1:120;
for ii = 1:size(entry_array,2)
    load results/resultsBaseline.mat 
    Vb_guess = zeros(1,9); % doesn't matter for this one    
    params(3) = params(3)+1000*entry_array(ii);
    num_buyers = sum(repmat(num_b',1,kw).*w_mat);
    buyerfix = 0;
    saveas = strcat('EntryCost',num2str(entry_array(ii)));
    filename = strcat('results/results',saveas,'.mat'); 
    if exist(filename, 'file')
        eval(['load results/results',saveas,' ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess;']);
    elseif exist(strcat('results/results','EntryCost',num2str(entry_array(ii)-1),'.mat'), 'file')
        eval(['load results/resultsEntryCost',num2str(entry_array(ii)-1),'.mat ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess;']);
    else 
        load results/resultsBaseline.mat ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess; 
    end
    model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,saveas,0, buyerfix, num_buyers);   
end    

for ii = 100:size(entry_array,2)
   load results/resultsBaseline.mat 
   Vb_guess = zeros(1,9);  
   params(3) = params(3)+1000*entry_array(ii);
   num_buyers = sum(repmat(num_b',1,kw).*w_mat);
   buyerfix = 1; 
   saveas = strcat('EntryCost',num2str(entry_array(ii)),'Buyerfix');
   filename = strcat('results/results',saveas,'.mat');
   if exist(filename, 'file')
        eval(['load results/results',saveas,' ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess;']);
    elseif exist(strcat('results/results','EntryCost',num2str(entry_array(ii)-1),'Buyerfix.mat'),'file')
        eval(['load results/resultsEntryCost',num2str(entry_array(ii)-1),'.mat ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess;']);
   else
       eval(['load results/resultsBaseline.mat ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess;']);
   end
   model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,saveas,0,buyerfix, num_buyers, Vb_guess);      
end


%{ 

%%%%%%%%%%%%%%%%%%%%%% other counterfactual: commission rate
%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%% % this was ran with an error
%%%%%%%%%%%%%%%%%%%%%% before where ommissions were ranging from 1% to 40%
%%%%%%%%%%%%%%%%%%%%%% instead of 1% to 4% on a finer grid.
clear;
psi_array = 0.01:0.001:0.04;
for ii = 1:size(psi_array,2)
    load results/resultsBaseline.mat 
    Vb_guess = zeros(1,9); % doesn't matter for this one      
    num_buyers = sum(repmat(num_b',1,kw).*w_mat);   
    buyerfix = 0;
    psi = psi_array(ii);
    params = ([8.6 1.7 20000 .3]);
    
    saveas = strcat('Commission',num2str(ii));
    filename = strcat('resultsCommission',num2str(ii),'.mat'); 
    %if exist(filename, 'file')
    %    eval(['load results/results',saveas,' num_b ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb;']);
    %    Vb_guess = Vb;
    %else
    %   load results/resultsBaseline.mat num_b kw w_mat params ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit
    %   num_buyers = sum(repmat(num_b',1,kw).*w_mat);
    %   Vb_guess = zeros(1,9);
    %end
    model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,saveas,0, buyerfix, num_buyers, Vb_guess);   
    
    
    load results/resultsBaseline.mat 
    Vb_guess = zeros(1,9);      
    num_buyers = sum(repmat(num_b',1,kw).*w_mat);   
    buyerfix = 1;
    psi = psi_array(ii);
    params = ([8.6 1.7 20000 .3]);
    saveas = strcat('Commission',num2str(ii),'Buyerfix');
    filename = strcat('resultsCommission',num2str(ii),'.mat'); 
%     if exist(filename, 'file')
%         eval(['load results/results',saveas,' num_b ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb;']);
%         Vb_guess = Vb;
%     else
%        eval(['load results/resultsCommission',num2str(ii),'.mat num_b kw w_mat params ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit;']);
%     end
    model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,saveas,0,buyerfix, num_buyers, Vb_guess);   
end

%%%%%%%%%%%%%%%%%%%%%% other counterfactual: client info %%%%%%%%%%%%%%%%%%%%%%
delta_array = 0.01:0.01:0.9;
for ii = 1:size(delta_array,2)
    load results/resultsBaseline.mat 
    Vb_guess = zeros(1,9); % doesn't matter for this one
    params(4) = delta_array(ii);
    num_buyers = sum(repmat(num_b',1,kw).*w_mat);
    buyerfix = 0;
    saveas = strcat('ListDist',num2str(ii));
    filename = strcat('results/results',saveas,'.mat'); 
%     if exist(filename, 'file')
%         eval(['load results/results',saveas,' ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess;']);
%     else
%        load results/resultsBaseline.mat ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess; 
%     end
    model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,saveas,0, buyerfix, num_buyers);   
    
   load results/resultsBaseline.mat
   num_buyers = sum(repmat(num_b',1,kw).*w_mat); 
   Vb_guess = zeros(1,9); % doesn't matter for this one
   params(4) = delta_array(ii); 
   buyerfix = 1;    
   saveas = strcat('ListDist',num2str(ii),'Buyerfix');
   filename = strcat('results/results',saveas,'.mat');
%    if exist(filename, 'file')
%         eval(['load results/results',saveas,' ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb;']);
%         Vb_guess = Vb;
%    else
%        eval(['load results/resultsListDist',num2str(ii),'.mat ke State kh ka T ks kw l_bar init psi alph bet S_state q_vec q license_limit Vb_guess;']);
%    end
   model_passive_learn_dyn_prices_poisson(params, ke, State, kh, ka, T, ks, kw, l_bar, init, psi, alph, bet, S_state, q_vec, q,license_limit,saveas,0,buyerfix, num_buyers, Vb_guess);      
end

%}
